package com.hippo.core.ui.components

import android.util.Log
import android.widget.NumberPicker
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.hippo.core.config.AppStoreKey
import com.hippo.core.core.manager.AppStore
import com.hippo.core.ui.theme.HippoTheme

@Composable
fun TimeLayout(
    defaultHour: Int = 0,
    defaultMinute: Int = 0,
    onHourChange: (Int) -> Unit,
    onMinuteChange: (Int) -> Unit,
    modifier: Modifier = Modifier,
) {

    val hour = AppStore.readInt(AppStoreKey.TimeFormat,24)


    val context = LocalContext.current

    val hourPicker = remember {
        NumberPicker(context).apply {
            minValue = 0
            maxValue = hour
            value = defaultHour
            setOnValueChangedListener { picker, oldVal, newVal ->
                onHourChange(newVal)
            }
        }
    }

    val minutePicker = remember {
        NumberPicker(context).apply {
            minValue = 0
            maxValue = 60
            value = defaultMinute
            setOnValueChangedListener { picker, oldVal, newVal ->
                onMinuteChange(newVal)
            }
        }
    }

    Row(
        modifier = modifier
            .height(200.dp)
            .fillMaxWidth()
            .background(
                color = Color.White,
                shape = MaterialTheme.shapes.medium
            )
    ) {

        Spacer(modifier = Modifier.weight(1f))

        AndroidView(
            factory = {
                hourPicker
            }
            , update = {
                Log.i("sss", "TimeLayout: it.value = ${it.value}  defaultHour = $defaultHour")
                hourPicker.value = defaultHour
            }
        )

        Spacer(modifier = Modifier.width(20.dp))

        AndroidView(
            factory = {
                minutePicker
            },
            update = {
                 minutePicker.value = defaultMinute
            }
        )

        Spacer(modifier = Modifier.weight(1f))

    }
}


@Preview(showBackground = true)
@Composable
private fun ItemLayoutPreview() {
    HippoTheme {
        TimeLayout(
            defaultMinute =  20,
            defaultHour = 30,
            onHourChange = {
                Log.i("sss", "ItemLayoutPreview: hour = $it")
            },
            onMinuteChange = {
                Log.i("sss", "ItemLayoutPreview: minute = $it")
            }
        )
    }
}